<?php
namespace app\company\controller;
use app\company\model\ReportLog as ReportLog;
use app\company\model\NormField as normModel;
use app\company\model\Sources as sourcesModel;
use app\company\model\NormCategory as categoryModel;


class Report extends Base
{
  public function index(){
    $log  = new ReportLog;
		$list = $log->order('id DESC')->paginate();
		$this->assign('list', $list);
    return $this->fetch();
  }

  public function add(){
    if(request()->isPost()){
        $data = input('post.');
        $ins['title']= $data['title'];
        unset($data['title']);
        $ins['source'] = $data['source'];
        unset($data['source']);
        $ins['data'] = serialize($data);
        $log = new ReportLog;
        $result = $log->save($ins);

         if($result){
		    	$this->success('添加成功','index');
		    }else{
		    	$this->error('添加失败');
		    }
    }else{
      $normCate = new categoryModel;
      $norm = new normModel;
      $sources = new sourcesModel;
      $list =  $normCate->where('status',1)->field('id,name')->order('sort,id')->select();
      foreach ($list as $key => $value) {
        $list[$key]['fields'] = $norm->where('status',1)->where('category',$value['id'])->field('id,category,name,field')->order('sort,id')->select();
      }
      $source =$sources->where('status','1')->field('id,name')->order('sort,id')->select();
      $this->assign('source',$source);
      $this->assign('list',$list);
      return $this->fetch();
    }
    
  }
  public function show($id){
      $log  = new ReportLog;
      $item =$log::get($id);
      $item->data = unserialize($item->data);

      $normCate = new categoryModel;
      $cate =  $normCate->where('status',1)->field('id,name')->order('sort,id')->select();
      foreach ($cate as $key => $value) {
        $list[$value['id']]['name'] = $value['name'];
      }

      foreach ($item->data as $key => $value) {

        $list[$value['cid']]['data'][$key] = $value;

       
      }


      $this->assign('cate',$cate);
			$this->assign('list',$list);
			return $this->fetch();
  }
  public function delete($id){
		$log  = new ReportLog;
		$result = $log::destroy($id);
		if($result){
			$this->success('删除成功','index');
		}else{
			$this->error('删除失败');
		}
	}
}